from django.db.models import Q
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView

from course import models
from course.models import Category, Course
from course.ser import CateGorySer, CourseSer, CateForySer, CourseDetailSer, CourseIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

from utils.redis import toolredis


# Create your views here.
class HomeCat(APIView):
    def get(self,request):
        catelist=Category.objects.filter(parent=None,isdel=False).order_by('sort')
        ser=CateGorySer(catelist,many=True)
        data=ser.data
        for i in data:
            i['children']=i['children'][:3]
        return Response({"msg":"success","code":200,"data":data})
class CateChild(APIView):
    def get(self,request):
        parent=request.query_params.get('parent')
        info=Category.objects.filter(id=parent,isdel=False).first()
        ser=CateGorySer(info)
        data=ser.data
        data['course']=[]
        return Response({"msg":"success","code":200,"data":data})
class Direction(APIView):
    def get(self,request):
        onecate=Category.objects.filter(parent=None,isdel=False).order_by('sort')
        ser=CateGorySer(onecate,many=True)
        return Response({"msg":"success","code":200,"data":ser.data})
class Categorys(APIView):
    def get(self,request):
        parent=request.query_params.get('parent')
        if parent=='0':
            catelist=Category.objects.filter(parent__isnull=False,isdel=False).order_by('sort')
        else:
            catelist=Category.objects.filter(parent=parent,isdel=False).order_by('sort')
        ser=CateForySer(catelist,many=True)
        return Response({"msg":"success","code":200,"data":ser.data})
class CoursePage(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
class CourseList(ListAPIView):
    queryset = Course.objects.filter(isdel=False)
    serializer_class = CourseSer
    pagination_class = CoursePage
    def get_queryset(self):
        direction=self.request.query_params.get('direction')
        category=self.request.query_params.get('category')
        ordering=self.request.query_params.get('ordering')
        query=Q(isdel=False)
        if direction!="0"and category!="0":
            query&=Q(category=category)
        if direction!="0" and category=="0":
            child=Category.objects.filter(parent=direction,isdel=False).values_list('id',flat=True)
            query &= Q(category__in=child)
        if ordering:
            return self.queryset.filter(query).order_by(ordering)
        return self.queryset.filter(query)
class Detail(APIView):
    def get(self,request):
        id=request.query_params.get('id',None)
        if not id:
            return Response({"msg":"id不能为空","code":400})
        info=Course.objects.filter(id=id,isdel=False).first()
        ser=CourseDetailSer(info)
        return Response(ser.data)

#索引视图
class IndexView(HaystackViewSet):
    index_models=[models.Course]
    serializer_class=CourseIndexSerializer
    pagination_class = CoursePage
    def list(self, request, *args, **kwargs):
        word = request.query_params.get('text', None)
        if word:
            toolredis.setword(word)
        return super().list(request, *args, **kwargs)
class HotWord(APIView):
    def get(self,request):
        return Response(toolredis.getword())